package com.txzt.gaea.flow.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.page.PageInfoBT;
import com.txzt.gaea.common.util.StringUtil;
import com.txzt.gaea.flow.dto.FlowDefinitionQueryDTO;
import com.txzt.gaea.flow.model.FlowBizModel;
import com.txzt.gaea.flow.model.FlowDefinition;
import com.txzt.gaea.flow.model.FlowStepLog;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.flow.service.IFlowStepLogService;
import com.txzt.gaea.flow.vo.FlowDefinitionVo;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/flow")
public class FlowController extends BaseController {

    @Autowired
    private FlowService flowService;
    @Autowired
    private IFlowStepLogService flowStepLogService;

    @PostMapping(value = "/page/list")
    public RtnInfo<?> pageList(@RequestBody FlowDefinitionQueryDTO flowDefinitionQueryDTO){
        if (!StringUtil.isEmpty(flowDefinitionQueryDTO.getStartDate())){
            flowDefinitionQueryDTO.setStartDate(flowDefinitionQueryDTO.getStartDate() + " 00:00:00");
        }
        if (!StringUtil.isEmpty(flowDefinitionQueryDTO.getEndDate())){
            flowDefinitionQueryDTO.setEndDate(flowDefinitionQueryDTO.getEndDate() + " 23:59:59");
        }
        return flowService.pageList(flowDefinitionQueryDTO);
    }

    @PostMapping(value = "/add")
    public RtnInfo<?> add(@RequestBody FlowDefinition flowDefinition){
        if (null != flowDefinition && null == flowDefinition.getId()){
            //查重
            if (!flowService.checkFlowNameAndFlag(flowDefinition)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查流程名称和流程标识");
            }
            int num = flowService.save(flowDefinition);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo<?> getInfoById(@PathVariable Long id){
        return RtnInfo.success(flowService.getFlowInfo(id));
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> edit(@RequestBody FlowDefinition flowDefinition){
        if (null != flowDefinition && null != flowDefinition.getId()){
            int num = flowService.update(flowDefinition);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }
    @PostMapping(value = "/start")
    public RtnInfo<?> startFlow(@RequestBody Map<String,Object> map){
        String id = String.valueOf(map.get("bizId"));
        Long bizFlowId = flowService.startFlow(Long.parseLong(id),(String) map.get("bizTable"),
                (String) map.get("bizStatusCol"), (Integer) map.get("bizBackStatus"),
                (Integer) map.get("bizPassStatus"),(String) map.get("flowFlag"));
        return RtnInfo.SUCCESS;
    }

    @PostMapping(value = "/start/upgrade")
    public RtnInfo<?> startFlowUpgrade(@RequestBody Map<String,Object> map){
        String id = String.valueOf(map.get("bizId"));
        Long bizFlowId = flowService.startFlowUpgrade(Long.parseLong(id),(String) map.get("bizTable"),
                (String) map.get("bizStatusCol"), (Integer) map.get("bizBackStatus"),(Integer) map.get("bizPassStatus"),
                (Integer) map.get("bizRejectStatus"),(Integer) map.get("bizRevocationStatus"),
                (String) map.get("flowFlag"),(Long) map.get("deptId"));
        return RtnInfo.SUCCESS;
    }

    /**
     * 流程流转-节点修改及抄送
     *
     * @param flowBizModel
     * @return
     */
    @PostMapping(value = "/next/upgrade")
    public RtnInfo<?> nextUpgrade(@RequestBody FlowBizModel flowBizModel){
        boolean flag = flowService.nextStepUpgrade(flowBizModel);
        if (flag) {
            return RtnInfo.SUCCESS;
        }else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 流程流转-撤回
     *
     * @param flowBizId
     * @return
     */
    @GetMapping(value = "/revocation")
    public RtnInfo<?> revocation(@RequestParam Long flowBizId,@RequestParam Long stepId){
        boolean flag = flowService.revocation(flowBizId,stepId);
        if (flag) {
            return RtnInfo.SUCCESS;
        }else {
            return RtnInfo.SERVER_ERROR;
        }

    }

    @PostMapping(value = "/next")
    public RtnInfo<?> next(@RequestBody Map<String,Object> map){
        String id = String.valueOf(map.get("bizId"));
        boolean a = flowService.nextStep(Long.parseLong(id),(String) map.get("flowFlag"),
                (Integer) map.get("status"),(String) map.get("comment"));
        return RtnInfo.SUCCESS;
    }

    @PostMapping(value = "/next/step")
    public RtnInfo<?> nextStep(@RequestBody Map<String,Object> map){
        String bizFlowId = String.valueOf(map.get("bizFlowId"));
        flowService.nextStep(Long.parseLong(bizFlowId),(Integer) map.get("status"),(String) map.get("comment"));
        return RtnInfo.SUCCESS;
    }

    @GetMapping(value = "/biz/step/list/{bizFlowId}")
    public RtnInfo<?> getCurrentStep(@PathVariable Long bizFlowId){
        return RtnInfo.success(flowService.getFlowBizStep(bizFlowId));
    }

    /**
     * 查询流程步骤
     * @return
     */
    @GetMapping(value = "/selectFlowStep")
    public RtnInfo<?> selectFlowStep(@RequestParam String flowType,@RequestParam String flowFlag) {
        return RtnInfo.success(flowService.showStepList(flowType,flowFlag));
    }


    /**
     * 分页查询流程日志
     * @param pageNumber
     * @param pageSize
     * @param flowStepLog
     * @return
     */
    @GetMapping(value = "/log")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, FlowStepLog flowStepLog) {
        PageQuery<FlowStepLog> pageList = flowStepLogService.pageQuery(new PageQuery<>(pageNumber, pageSize, flowStepLog));
        pageList.getList().forEach(f ->{
            //翻译操作人
            f.setOperatorName(TranslateUtil.getUserName(f.getOperator()));
            //翻译抄送人
            f.setCcUserName(TranslateUtil.getUserNames(f.getCcUser()));
        });
        return RtnInfo.success(pageList);
    }

}
